package view;
import service.CountWordService;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MainFrame
{
    private Frame f;
    private MenuBar bar;
    private Menu fileMenu,subMenu;
    private MenuItem closeItem,subItem1,subItem2;
    private MenuItem openItem,saveItem;

    private CountWordService service;

    //定义打开和保存对话框
    private FileDialog openDia,saveDia;

    //设置文本区域来保存打开的数据
    private TextArea ta;

    private File file;

    public MainFrame()
    {
        init();
    }

    public void init()
    {
        service = new CountWordService();

        f = new Frame("汉字字频计数");
        f.setBounds(300,100,500,600);
        //f.setLayout(new FlowLayout());

        bar = new MenuBar();

        ta = new TextArea();

        fileMenu = new Menu("文件");

        openItem = new MenuItem("打开");
        saveItem = new MenuItem("分析");
        closeItem = new MenuItem("退出");



        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(closeItem);
        bar.add(fileMenu);

        f.setMenuBar(bar);

        //默认模式为 FileDialog.LOAD
        openDia = new FileDialog(f,"My open",FileDialog.LOAD);
        saveDia = new FileDialog(f,"My save",FileDialog.SAVE);

        f.add(ta);

        myEvent();

        f.setVisible(true);


    }

    private void myEvent()
    {
        saveItem.addActionListener(new ActionListener()
        {
            //设置保存文件的功能
            public void actionPerformed(ActionEvent e)
            {
                if(file == null)//文件不存在情况下 创建文件
                {
                    saveDia.setVisible(true);
                    String dirPath = saveDia.getDirectory();
                    String fileName = saveDia.getFile();

                    if(dirPath == null || fileName == null)
                        return ;
                    file = new File(dirPath,fileName);
                }

                try
                {
                    BufferedWriter bufw = new BufferedWriter(new FileWriter(file));

                    String text = ta.getText();

                    bufw.write(text);

                    bufw.close();
                }
                catch (IOException ex)
                {
                    throw new RuntimeException("file save error!");
                }
            }
        });

        openItem.addActionListener(new ActionListener()
        {
            //设置打开文件功能
            public void actionPerformed(ActionEvent e)
            {
                openDia.setVisible(true);
                String dirPath = openDia.getDirectory();//获取文件路径
                String fileName = openDia.getFile();//获取文件名称
                //System.out.println(dirPath +"++"+ fileName);

                //如果打开路径 或 目录为空 则返回空
                if(dirPath == null || fileName == null)
                    return ;

                ta.setText("");//清空文本

//                file = new File(dirPath,fileName);
//
//                try
//                {
//                    BufferedReader bufr = new BufferedReader(new FileReader(file));
//
//                    StringBuffer sb = new StringBuffer();
//                    String line = null;
//
//                    while( (line = bufr.readLine())!= null)
//                    {
//                        ta.append(line +"\r\n");
//                    }
//                    bufr.close();
//                }
//                catch (IOException ex)
//                {
//                    throw new RuntimeException("read file field！");
//                }
            }
        });

        closeItem.addActionListener(new ActionListener()
        {
            //设置退出功能
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }


}